package com.zjxf.controller;

import com.alibaba.fastjson.JSONObject;
import com.zjxf.base.bean.po.CmsSchoolNews;
import com.zjxf.base.bean.po.CmsSchoolTutor;
import com.zjxf.base.bean.po.UmsStoreSeller;
import com.zjxf.base.bean.ro.RoCmsSchoolNews;
import com.zjxf.base.bean.ro.RoCmsSchoolTutor;
import com.zjxf.base.bean.vo.VoCmsSchoolNews;
import com.zjxf.base.common.JSONResponse;
import com.zjxf.base.common.OperationException;
import com.zjxf.controller.common.BaseController;
import com.zjxf.service.CmsSchoolNewsService;
import com.zjxf.service.CmsSchoolTutorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * 长征大学首页内容管理Controller
 * Created by lzz on 2021/4/1.
 */
@Api(tags = "LmSchoolHomeController", description = "长征大学首页内容管理")
@AllArgsConstructor
@RestController
@RequestMapping("/app/lmhome/")
public class LmSchoolHomeController extends BaseController {

    private final CmsSchoolNewsService cmsSchoolNewsService;

    private final CmsSchoolTutorService cmsSchoolTutorService;


    /**
     * banner详情
     *
     * @param id schoolBanner的编号
     * @return JSONResponse
     * @throws OperationException banner不存在
     */
    @ApiOperation(value = "banner详情")
    @PostMapping(value = "bannerDetail/{id}")
    public JSONResponse getEntity(@NotNull @PathVariable Long id) throws OperationException {
        CmsSchoolNews news = cmsSchoolNewsService.getEntity(id);
        RoCmsSchoolNews.Details details = cmsSchoolNewsService.getEntityRo(news);
        return success(details);
    }

    /**
     * banner列表展示
     *
     * @param filter 过滤实体
     * @return JSONResponse
     */
    @ApiOperation(value = "banner列表展示")
    @PostMapping(value = "banner")
    public JSONResponse bannerPage(@Valid VoCmsSchoolNews.filter filter) {
        //TODO 后期不要用这个接口 这个接口要通过shiro登录的主体获取店铺id，app端应该通过定位获取省市区来定位商铺id
        Page<CmsSchoolNews> cmsSchoolNewsPage = cmsSchoolNewsService.page(filter, getUmsStoreSeller());
        JSONObject jsonData = cmsSchoolNewsService.pageRo(cmsSchoolNewsPage);
        return success(jsonData);
    }

    //模拟的商家端用户
    public UmsStoreSeller getUmsStoreSeller() {
        UmsStoreSeller umsStoreSeller = new UmsStoreSeller();
        umsStoreSeller.setStoreId(1L);
        umsStoreSeller.setId(1L);
        return umsStoreSeller;
    }

    /**
     * 获取学校新闻实体信息
     *
     * @param id 新闻编号
     * @return JSONResponse
     * @throws OperationException 学校新闻不存在
     */
    @ApiOperation(value = "获取学校新闻实体信息")
    @PostMapping(value = "news/{id}")
    public JSONResponse getNewsEntity(@NotNull @PathVariable Long id) throws OperationException {
        CmsSchoolNews cmsSchoolNews = cmsSchoolNewsService.getEntity(id);
        RoCmsSchoolNews.Details details = cmsSchoolNewsService.getEntityRo(cmsSchoolNews);
        return success(details);
    }

    /**
     * 新闻头条列表展示
     *
     * @param filter 过滤实体
     * @return JSONResponse
     */
    @ApiOperation(value = "新闻头条列表展示")
    @PostMapping(value = "newsbanner")
    public JSONResponse newsPage(@Valid VoCmsSchoolNews.filter filter) {
        Page<CmsSchoolNews> cmsSchoolNewsPage = cmsSchoolNewsService.page(filter, getUmsStoreSeller());
        JSONObject conversion = cmsSchoolNewsService.pageRo(cmsSchoolNewsPage);
        return success(conversion);
    }

    /**
     * 获取学校导师实体信息
     *
     * @param id 学校导师编号
     * @return JSONResponse
     * @throws OperationException 学校导师不存在
     */

    @ApiOperation(value = "获取学校导师实体信息")
    @PostMapping(value = "teacher/{id}")
    public JSONResponse getTeacherEntity(@NotNull @PathVariable Long id) throws OperationException {
        CmsSchoolTutor cmsSchoolTutor = cmsSchoolTutorService.getEntity(id);
        RoCmsSchoolTutor.Details details = cmsSchoolTutorService.getEntityRo(cmsSchoolTutor);
        return success(details);
    }
}
